<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Request;
use \think\Db;
use \think\facade\Session;

/*博客*/
class Classify extends Base
{  
    /*列表*/
	  public function index(Request $request){
		  $this->check_session();
        /*左侧导航*/
      $data_get=$request->get()['nav'];

      $data=$this->navleft($data_get);
    
      $this->assign($data);

      return $this->fetch();
	  }

    public function get_data(Request $request){
        $this->check_session();
        $get=$request->get();
        
        $model=new \app\admin\model\Classify();/*调用模型查询*/
        if(empty($get['sear'])){
           $model_data=$model->get_data($get['page'],$get['limit'],'');
        }else{
          $model_data=$model->get_data($get['page'],$get['limit'],$get['sear']);
        }
        
        foreach ($model_data['list'] as $key => $value) {
         
          $load_str='';
          $load=class_uplower($value['id']);
          for ($i=count($load)-1; $i >=0 ; $i--) { 
            $load_str.=$load[$i]['cname']."=>";
          }
          $model_data['list'][$key]['load']=rtrim($load_str,'=>');
          $model_data['list'][$key]['load_url']=get_class_uplower_url($value['id']);
        }

        $result=["code"=>0,'msg'=>"","count"=>$model_data['count'],"data"=>$model_data['list']];
        return json($result);
       
  }

	       /*新增 */
  public function change(Request $request){
    $this->check_session();
        $data=$request->post();
      if(empty($data)){
          $model=new \app\admin\model\Classify();/*调用模型查询*/
          $model_data=$model->get_sel(0);
          $seldata['sel']=$model_data;
          $this->assign($seldata);
          return $this->fetch();
        }else{
          if(empty($data['cname'])){
            $result=['status'=>'0','msg'=>'请填写分类名称'];
            return json($result);
          }
          $model=new \app\admin\model\Classify();/*调用模型查询*/
          $model_data=$model->insert_data($data['cname'],$data['fid'],$data['level'],$data['newname']);
          if($model_data==1){
                $result=['status'=>'1','msg'=>'操作成功'];
                return json($result);
           }else{
                $result=['status'=>'0','msg'=>'操作失败系统错误'];
                return json($result);
           }
        }
  }
    
     /*删除*/
    public function del(Request $request){
        $this->check_session();
        $data=$request->post();
        if(empty($data['id'])){
            $result=['status'=>'0','msg'=>'操作有误'];
            return json($result);
        }
        $model=new \app\admin\model\Classify();/*调用模型查询*/
        $model_data=$model->delete_data($data['id']);
        if($model_data!=1){
            $result=['status'=>'0','msg'=>'删除失败'];
            return json($result);
        }else{
            $result=['status'=>'1','msg'=>'删除成功'];
            return json($result);
        }
    }

    /*获取联动数据*/
    public function sel(Request $request){
      $this->check_session();
      $val=$request->post()['val'];
      $model=new \app\admin\model\Classify();/*调用模型查询*/
      $model_data=$model->get_sel($val);
      $str="<option value='0'>请选择分类</option>";
      foreach ($model_data as $key => $value) {
        $str.="<option value='".$value['id']."'>".$value['cname']."</option>";
      }
      return $str;
    }

    /*逆向查询*/
    public function get_cascade_data($fid){
      $class_model=new \app\admin\model\Classify();/*调用模型查询*/
      $class_model_data=$class_model->get_cascade_data($fid);
      return $class_model_data;
    }

    /*进入编辑页面*/
    public function edit(Request $request){
      $this->check_session();
      $post=$request->post();
      if(empty($post)){
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
        $get=$request->get();
        $class_model=new \app\admin\model\Classify();/*调用模型查询*/
        $get_one=$class_model->get_one($get['id']);
        $class_model_data=$this->get_cascade_data($get_one['fid']);
        krsort($class_model_data);
        $data['fl']=$class_model_data;
        $data['one']=$get_one;
        $this->assign($data);
        return $this->fetch();
      }else{
        if(empty($post['cname'])){
          $result=['status'=>'0','msg'=>'请填写分类名称'];
          return json($result);
        }
        $model=new \app\admin\model\Classify();/*调用模型查询*/
        $model_data=$model->update_data($post['id'],$post['cname'],$post['fid'],$post['level'],$post['newname']);
        if($model_data==1){
          $result=['status'=>'1','msg'=>'操作成功'];
          return json($result);
        }else{
          $result=['status'=>'0','msg'=>'操作失败系统错误'];
          return json($result);
        }

      }
    }
  


    public function zhedit(Request $request){
      $this->check_session();
      $post=$request->post();
      if(empty($post)){
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
        $get=$request->get();
        $class_model=new \app\admin\model\Classify();/*调用模型查询*/
        $get_one=$class_model->get_one($get['id']);
        $data['one']=$get_one;
        $data['post']=$post;
        $this->assign($data);
        return $this->fetch();
      }else{
        $model=new \app\admin\model\Classify();/*调用模型查询*/
        $model_data=$model->update_zhedit($post['id'],$post['zh_des'],$post['zh_des_s'],$post['zh_h'],$post['zh_title'],$post['zh_keyword'],$post['zh_description'],$post['sel_zh_title'],$post['sel_zh_keyword'],$post['sel_zh_description'],$post['zh_d_title'],$post['zh_d_keyword'],$post['zh_d_description'],$post['zh_content']);
        if($model_data==1){
          $result=['status'=>'1','msg'=>'操作成功'];
          return json($result);
        }else{
          $result=['status'=>'0','msg'=>'操作失败系统错误'];
          return json($result);
        }
      }  
    }


    public function enedit(Request $request){
      $this->check_session();
      $post=$request->post();
      if(empty($post)){
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
        $get=$request->get();
        $class_model=new \app\admin\model\Classify();/*调用模型查询*/
        $get_one=$class_model->get_one($get['id']);
        $data['one']=$get_one;
        $data['post']=$post;
        $classify_content=Db::table('lily_classify_content')->where('class_id',$get['id'])->find();
        $data['classify_content']=$classify_content;
        $this->assign($data);
        return $this->fetch();
      }else{
        $find_content=Db::table('lily_classify_content')->field('id')->where('class_id',$post['id'])->find();
        /*无数据添加，有数据更改*/
        if(empty($find_content)){

          $insert_data=[
            'class_id'=>$post['id'],
            'tab_title_des'=>$post['tab_title_des'],
            'total_des'=>$post['total_des'],
            'filter_total_des'=>$post['filter_total_des'],
            'block_des'=>$post['block_des'],
            'detail_des'=>$post['detail_des']
          ];
          $model_data=Db::table('lily_classify_content')->insert($insert_data);
          if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
          }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
          }

        }else{
          $update_data=[
            'tab_title_des'=>$post['tab_title_des'],
            'total_des'=>$post['total_des'],
            'filter_total_des'=>$post['filter_total_des'],
            'block_des'=>$post['block_des'],
            'detail_des'=>$post['detail_des']
          ];
          $model_data=Db::table('lily_classify_content')->where('class_id',$post['id'])->update($update_data);
          if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
          }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
          }
        }
      } 
    }

    public function tdk(Request $request){
      $this->check_session();
      $post=$request->post();
      if(empty($post)){
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
        $get=$request->get();
        $class_model=new \app\admin\model\Classify();/*调用模型查询*/
        $get_one=$class_model->get_one($get['id']);
        $data['one']=$get_one;
        $data['post']=$post;
        $classify_seo=Db::table('lily_classify_seo')->where([['class_id','=',$get['id']],['status','=',1]])->find();
        $data['classify_seo']=$classify_seo;
        $this->assign($data);
        return $this->fetch();
      }else{
        $find_content=Db::table('lily_classify_seo')->field('id')->where('class_id',$post['id'])->find();
        /*无数据添加，有数据更改*/
        if(empty($find_content)){

          $insert_data=[
            'class_id'=>$post['id'],
            'title'=>$post['title'],
            'keywords'=>$post['keywords'],
            'description'=>$post['description'],
            'filter_title'=>$post['filter_title'],
            'filter_keywords'=>$post['filter_keywords'],
            'filter_description'=>$post['filter_description']
          ];
          $model_data=Db::table('lily_classify_seo')->insert($insert_data);
          if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
          }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
          }

        }else{
          $update_data=[
            'title'=>$post['title'],
            'keywords'=>$post['keywords'],
            'description'=>$post['description'],
            'filter_title'=>$post['filter_title'],
            'filter_keywords'=>$post['filter_keywords'],
            'filter_description'=>$post['filter_description']
          ];
          $model_data=Db::table('lily_classify_seo')->where('class_id',$post['id'])->update($update_data);
          if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
          }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
          }
        }
      } 
    }

    public function dan(Request $request){
      $this->check_session();
      $post=$request->post();
      if(empty($post)){
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
        $get=$request->get();
        $class_model=new \app\admin\model\Classify();/*调用模型查询*/
        $get_one=$class_model->get_one($get['id']);
        $data['one']=$get_one;
        $data['post']=$post;
        $dan_view=Db::table('lily_classify_dan_view')->where([['class_id','=',$get['id']],['status','=',1]])->find();
        $data['dan_view']=$dan_view;
        $this->assign($data);
        return $this->fetch();
      }else{
        $find_content=Db::table('lily_classify_dan_view')->field('id')->where('class_id',$post['id'])->find();
        /*无数据添加，有数据更改*/
        if(empty($find_content)){

          $insert_data=[
            'class_id'=>$post['id'],
            'view_name'=>$post['view_name'],
            'content'=>$post['content']
          ];
          $model_data=Db::table('lily_classify_dan_view')->insert($insert_data);
          if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
          }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
          }

        }else{
          $update_data=[
            'view_name'=>$post['view_name'],
            'content'=>$post['content']
          ];
          $model_data=Db::table('lily_classify_dan_view')->where('class_id',$post['id'])->update($update_data);
          if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
          }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
          }
        }
      } 
    }

    public function edit_class(Request $request){
      $this->check_session();
      $post=$request->post();
      if(empty($post)){
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
        $get=$request->get();
        $class_model=new \app\admin\model\Classify();/*调用模型查询*/
        $get_one=$class_model->get_one($get['id']);
        $data['one']=$get_one;
        $data['post']=$post;
        $classify=Db::table('lily_classify')->where([['id','=',$get['id']],['status','=',1]])->find();
        $route=Db::table('lily_route_classify')->select();
        $data['classify']=$classify;
        $data['route']=$route;
        $this->assign($data);
        return $this->fetch();
      }else{
        
          $update_data=[
            'cname'=>$post['cname'],
            'route_name'=>$post['route_name'],
            'alias_name'=>$post['alias_name'],
            'seq'=>$post['seq'],
            'route_id'=>$post['route_id'],
            'is_pay'=>$post['is_pay'],
            'is_tag'=>$post['is_tag'],
            'is_show'=>$post['is_show'],
            'is_sitemap'=>$post['is_sitemap'],
            'is_show_left'=>$post['is_show_left'],
            'img'=>$post['img']
          ];
          
          $model_data=Db::table('lily_classify')->where('id',$post['id'])->update($update_data);
          if($model_data==1){
            $result=['status'=>'1','msg'=>'操作成功'];
            return json($result);
          }else{
            $result=['status'=>'0','msg'=>'操作失败系统错误'];
            return json($result);
          }
        
      } 
    }




}	